AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( "shared.lua" )

ENT.Speed = 0

function ENT:Initialize()
	self.Entity:SetModel("models/crossbow_bolt.mdl")
end

function ENT:SetWeapon (wpn)
	self.Weapon = wpn
end

function ENT:SetSpeed (spd)
	self.Speed = spd
end

function ENT:SetDamage (dmg)
	self.Damage = dmg
end

function ENT:Think()
	if self.DeathTime then
		if self.DeathTime < CurTime() then self.Entity:Remove() end
	else
		local trc = util.QuickTrace (self:GetPos(), self:GetAngles():Forward() * self.Speed, {self.Entity, self:GetOwner()})
		if trc.Hit then
			if trc.Entity and trc.Entity:IsValid() and trc.Entity:IsNPC() then
				--trc.Entity:TakeDamage (self.Damage, self:GetOwner(), self.Weapon)
				local bullet = {} 
			 	bullet.Num 		= 1
			 	bullet.Src 		= self.Entity:GetPos()
				bullet.Dir 		= self.Entity:GetAngles():Forward()
			 	bullet.Spread 	= Vector (0, 0, 0)
			 	bullet.Tracer	= 0
			 	bullet.Force	= 30
			 	bullet.Damage	= self.Damage
				bullet.Attacker	= self:GetOwner()
				
				self.Weapon:FireBullets (bullet)
			else
				self.DeathTime = CurTime() + 5
				self:SetPos (trc.HitPos - self:GetAngles():Forward() * 8)
			end
		else
			self:SetPos (trc.HitPos)
		end
		self.Entity:NextThink (CurTime() + 0.01)
		return true
	end
end
